package chatting4.Utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Optional;

import chatting4.Messages.Message;

public class MsgUtils {

	public static Optional<Message<?>> readMsg(InputStream inputStream){
		try {
			ObjectInputStream objectInputStream = new ObjectInputStream(inputStream);
			Message<?> res = (Message<?>) objectInputStream.readObject();
			return Optional.ofNullable(res);
		} catch (IOException | ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return Optional.empty();
	}
	
	
	public static void writeMsg(OutputStream outputStream,Message<?> message) {
		try {
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
			objectOutputStream.writeObject(message);
		}catch(IOException e) {
			e.printStackTrace();
		}
	}

}
